<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>分发密码</title>
	<link rel="stylesheet" href="${pageContext.request.contextPath}/lib/layui/css/layui.css">
</head>
<body>
<form class="layui-form">
	<div class="layui-row">
		<div class="layui-form-item">
			<div class="layui-col-xs12 layui-col-sm12 layui-col-md12">
				<div class="layui-input-block">
					<font color="red">*目前还剩余<span style="font-size:40px;" id="residueTicketNum"></span>次生成权限,请谨慎使用</font>
				</div>
			</div>
		</div>
		<div class="layui-col-xs6 layui-col-sm6 layui-col-md6">
			<label class="layui-form-label">发票类型:</label>
			<div class="layui-input-block">
		      <select id="ticketType" name="ticketType" lay-filter="ticketType">
		        <!-- <option value="11" selected="selected">白色(社会车)</option>
		        <option value="12">紫色(环卫五吨)</option>
		        <option value="13">蓝色(环卫三吨)</option>
		        <option value="14">黄色(四分车)</option> -->
		        <c:forEach items="${typeDicDatas}" var="typeDicData">
		        	<option value="${typeDicData.dicDataValue}">${typeDicData.dicDataName}</option>
		        </c:forEach>
		      </select>
		    </div>
	    </div>
		<div class="layui-form-item">
		    <label class="layui-form-label">跟车人</label>
		    <div class="layui-input-block">
		     <select id="receiveId" name="receiveId" lay-filter="receiveId">
		        <c:forEach items="${receivePhones}" var="receivePhone">
		        	<option value="${receivePhone.receiveId}">${receivePhone.receiveName}:${receivePhone.receivePhone}</option>
		        </c:forEach>
		      </select>
		    </div>
		  </div>
		<div class="layui-form-item">
			<div class="layui-input-block">
				<button class="layui-btn" type="button" id="submit_btn">生成密码</button>
			</div>
		</div>
	</div>
</form>
</body>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script src="${pageContext.request.contextPath}/lib/layui/layui.all.js"></script>
<script>
var ctx = "${pageContext.request.contextPath}";
var userInfoCustom = eval(${userInfoCustom});
var layer = layui.layer;
var form = layui.form;
var residueTicketNum=0;
var ticketTypeVal="";
var receiveIdVal="";
$(function(){
	ticketTypeVal = $("#ticketType").val();
	receiveIdVal = $("#receiveId").val();
	findResidueTicketNum(ticketTypeVal);
	$("#submit_btn").on("click",function(){
		if(residueTicketNum<1){
			layer.msg("请检查您选择的发票类型是否还有剩余!");
			return false;
		}
		if(receiveIdVal==null){
			layer.msg("请检查您是否选择了跟车人!");
			return false;
		}
		$.ajax({
		  type: 'POST',
		  url: ctx+"/pwd/generatePwd",
		  data: {
			  userId:userInfoCustom.userId,
			  ticketType:ticketTypeVal,
			  receiveId:receiveIdVal
		  },
		  success: function(data){
			  if(data.data && data.data.pwdValue){
				  layer.msg("生成的密码为:"+data.data.pwdValue, {
					time: 30000, //20s后自动关闭
			        btn: ['确定']
			      });
			  }else{
				  layer.msg("生成的密码失败,请联系管理员!", {
					time: 30000, //20s后自动关闭
			        btn: ['确定']
			      });
			  }
		  },
		  error:function(data){
			  layer.msg("系统错误,请联系管理员!");
		  }
		});
	});
	
});

form.on('select(ticketType)', function(data){
	ticketTypeVal=data.value;
	findResidueTicketNum(data.value);
}); 

form.on('select(receiveId)', function(data){
	receiveIdVal=data.value;
}); 

function findResidueTicketNum(ticketTypeValue){
	residueTicketNum=0;
	$.ajax({
	  type: 'POST',
	  url: ctx+"/ticket/findResidueTicketNum",
	  data: {
		  userId:userInfoCustom.userId,
		  ticketType:ticketTypeValue
	  },
	  success: function(data){
		  residueTicketNum=data.data.ticketNum;
		  $("#residueTicketNum").html(residueTicketNum);
	  },
	  error:function(data){
		  layer.msg("系统异常!!!");
	  }
	});
}



</script>
</html>